import cv2


def car_detect():
    img_path = "img/car2.jpeg"
    car_img = cv2.imread(img_path)

    grey_img = cv2.cvtColor(car_img, cv2.COLOR_BGR2GRAY)
    # cv2.imshow("greg", grey_img)
    # cv2.waitKey()
    # car file
    class_identifier_car = 'data/cars.xml'
    car_tracker = cv2.CascadeClassifier(class_identifier_car)

    car_coordinates = car_tracker.detectMultiScale(grey_img)
    print(car_coordinates)
    # start at: up Left  then up Down
    for (x, y, w, h) in car_coordinates:
        cv2.rectangle(car_img, (x, y), (x+w, x+h), (0, 255, 0), 2)

    cv2.imshow("Q5", car_img)
    cv2.waitKey()

def car_detect_vedio():
    img_path = "img/car2.jpeg"
    webcam = cv2.VideoCapture(0)
    class_identifier_car = 'data/cars.xml'
    car_tracker = cv2.CascadeClassifier(class_identifier_car)
    full_body_file = 'data/haarcascade_fullbody.xml'
    full_body = cv2.CascadeClassifier(full_body_file)
    while True:
        successful_frame_read, frame = webcam.read()
        # grey scaled
        if successful_frame_read:
            gray_scaled_img = cv2.cvtColor(frame,  cv2.COLOR_BGR2GRAY)
            # detect faces
            car_coordinates = car_tracker.detectMultiScale(gray_scaled_img)
            print(car_coordinates)
            # Draw rectangles around the faces
            # start at: up Left  then up Down
            for (x, y, w, h) in car_coordinates:
                cv2.rectangle(frame, (x, y), (x + w, x + h), (0, 255, 0), 2)

            cv2.imshow("Iron Man", frame)
            key = cv2.waitKey(1)
            # q for quit
            if key == 81 or key == 113:
                break
        else:
            continue

    webcam.release()
    print("finished")



if __name__ == '__main__':
   # car_detect()
    car_detect_vedio()